﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Runda.CMS.Common.Entity;
using Runda.CMS.Bll;
using Telerik.WebControls;
using Runda.CMS.Common.Utility;

namespace Runda.CMS.Portal.ContractManage
{
    public partial class SelectGift : BasePage
    {
        public override bool IsCheckAccessible
        {
            get
            {
                return false;
            }
        }
        public Guid ContractID
        {
            get
            {
                if (Request.QueryString["ContractID"] != null)
                {
                    return new Guid(Request.QueryString["ContractID"]);
                }
                return Guid.Empty;
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (ContractID == Guid.Empty)
                {
                    ClientScript.RegisterStartupScript(this.GetType(), "Error", "alert('参数有误!');window.close();", true);
                    return;
                }
                GenerateTreeView();
            }
        }
        private void GenerateTreeView()
        {
            this.RadTreeView1.Nodes.Clear();

            List<SqlDbParameter> parms = new List<SqlDbParameter>();

            List<CtrtInstrumentCategoryEntity> categoryEntitys = new CtrtInstrumentCategoryBll().GetAll<CtrtInstrumentCategoryEntity>(parms, "Order By categoryCode");

            List<CtrtInstrumentCategoryEntity> subCategoryEntitys = categoryEntitys.FindAll(delegate(CtrtInstrumentCategoryEntity e) { return e.parentGUID == Guid.Empty; });
            foreach (CtrtInstrumentCategoryEntity ent in subCategoryEntitys)
            {
                RadTreeNode node = CreateNode(ent.categoryName, ent.categoryName, false, ent.categoryGUID.ToString());
                RadTreeView1.Nodes.Add(node);

                RecursivelyPopulate(categoryEntitys, node);
            }
        }

        private RadTreeNode CreateNode(string text, string toolTip, bool expanded, string id)
        {
            RadTreeNode node = new RadTreeNode(text, id);
            node.Expanded = expanded;
            node.ContextMenuName = "Management";
            return node;
        }

        private void RecursivelyPopulate(List<CtrtInstrumentCategoryEntity> cateList, RadTreeNode node)
        {
            List<CtrtInstrumentCategoryEntity> SubDeptList = cateList.FindAll(delegate(CtrtInstrumentCategoryEntity e) { return e.parentGUID == new Guid(node.Value); });
            foreach (CtrtInstrumentCategoryEntity ent in SubDeptList)
            {
                RadTreeNode radTreeNode = CreateNode(ent.categoryName, ent.categoryName, false, ent.categoryGUID.ToString());
                node.Nodes.Add(radTreeNode);
                RecursivelyPopulate(cateList, radTreeNode);
            }
        }

        protected void btnQuery_Click(object sender, EventArgs e)
        {
            if (txtFilter.Text.Trim() != string.Empty)
            {
                ContractBll bll = new ContractBll();
                List<CtrtInstrumentEntity> list = bll.GetInstrumentEntities(txtFilter.Text.Trim());
                InstrumentList.DataSource = list;
                InstrumentList.DataBind();
            }
        }

        protected void RadTreeView1_NodeClick(object o, RadTreeNodeEventArgs e)
        {
            ContractBll bll = new ContractBll();
            List<CtrtInstrumentEntity> list = bll.GetInstrumentEntities(new Guid(e.NodeClicked.Value));
            InstrumentList.DataSource = list;
            InstrumentList.DataBind();
        }

        protected void btnOK_Click(object sender, EventArgs e)
        {
            List<CtrtContractGiftEntity> list = new List<CtrtContractGiftEntity>();
            CtrtContractGiftEntity entity = null;
            foreach (GridDataItem item in InstrumentList.Items)
            {
                CheckBox cbxSelect = item.FindControl("cbxSelect") as CheckBox;
                HiddenField hidType = item.FindControl("hidType") as HiddenField;
                if (cbxSelect != null)
                {
                    if (cbxSelect.Checked)
                    {
                        Guid instrumentGUID = new Guid(item.GetDataKeyValue("instrumentGUID").ToString());
                        object obj = item.GetDataKeyValue("Cost");
                        entity = new CtrtContractGiftEntity();
                        entity.InstrumentID = instrumentGUID;
                        entity.ContractID = ContractID;
                        entity.CreatedAt = DateTime.Now;
                        entity.CreatedBy = base.CurrentUserID;
                        entity.Cost =obj == null?0: Convert.ToDecimal(item.GetDataKeyValue("Cost").ToString());
                        entity.Deleted = false;
                        list.Add(entity);
                    }
                }
            }
            if (list.Count == 0)
            {
                ClientScript.RegisterStartupScript(this.GetType(), "null", "alert('请选择器械!');", true);
                return;
            }

            ContractBll bll = new ContractBll();
            bll.SelectGift(list);

            ClientScript.RegisterStartupScript(this.GetType(), "success", "window.close();", true);
        }
    }
}
